﻿using System;
using System.Text;
using Microsoft.WindowsMobile.Samples.Location;
using SimpleGPSApp.pl.portalkierowcy;
using IRQ.BaseFunctions;
using System.IO;

namespace SimpleGPSApp.Base
{
    public class Logger
    {
        private static DateTime lastTime = DateTime.Now;

        public static void LogPosition(GpsPosition position, server server)
        {
            switch (Root.ApplicationSettings.LogingType)
            {
                case LogType.File:
                    LogPositionToFile(position);
                    break;
                case LogType.Server:
                    LogPositionOnServer2(position, server);
                    break;
                case LogType.FileAndServer:
                    LogPositionToFile(position);
                    LogPositionOnServer2(position, server);
                    break;
                case LogType.Off:
                    break;
            }
        }

        private static void LogPositionToFile(GpsPosition position)
        {
            if (lastTime != position.Time)
            {
                StringBuilder sb = new StringBuilder();

                if (position.TimeValid)
                {
                    sb.Append(position.Time.ToString("yyyy-MM-dd"));
                    sb.Append("T");
                    sb.Append(position.Time.ToString("HH:mm:ss"));
                    sb.Append("Z");

                    lastTime = position.Time;
                }
                else
                {
                    sb.Append(DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd"));
                    sb.Append("T");
                    sb.Append(DateTime.Now.ToUniversalTime().ToString("HH:mm:ss"));
                    sb.Append("Z");

                    lastTime = DateTime.Now.ToUniversalTime();
                }
                sb.Append("_Lat=");

                sb.Append(position.Latitude.ToString());
                sb.Append("_Lng=");
                sb.Append(position.Longitude.ToString());
                sb.Append("_Alt=");
                sb.Append(position.SeaLevelAltitude.ToString());

                if (!ApplicationHelper.SaveTxtFile(Root.PositionLogFileName, FileMode.Append, sb.ToString(), true))
                {
                    ErrorLog.WriteError(Root.ErrorLogFileName, "Zapis danych do pliku nieudany");
                }
            }
        }

        /// <summary>
        /// using webservice from portalkierowcy.pl
        /// </summary>
        private static void LogPositionOnServer2(GpsPosition position, server server)
        {
            decimal speed = position.SpeedValid ? (decimal)position.Speed : 0;
           
            server.AddPositionWP(
                "walkuszmichal@wp.pl",
                "827ccb0eea8a706c4c34a16891f84e7b",
                "irq1234",
                (decimal)position.Latitude,
                (decimal)position.Longitude,
                (decimal)position.SeaLevelAltitude,
                0,
                speed,
                position.SatelliteCount,
                1,
                0,
                DateTime.Now);
        }
    }
}
